<template>
  <div class="ele-body">
    <el-card shadow="never">
      <!-- 搜索表单 -->
      <company-search @search="reload" />
      <!-- 数据表格 -->
      <ele-pro-table ref="table" :columns="columns" :datasource="datasource" :selection.sync="selection">
       
        <!-- 表头工具栏 -->
        <template v-slot:toolbar>
          <el-button size="small" type="primary" icon="el-icon-plus" class="ele-btn-icon" @click="openEdit()">
            新建
          </el-button>
          <el-button size="small" type="danger" icon="el-icon-delete" class="ele-btn-icon" @click="removeBatch">
            删除
          </el-button>
        </template>

        <!-- 图片列 -->
        <template v-slot:avatar="{ row }">
          <img :src="row.avatar" style="width: 50px; height: 50px; " />
        </template>
        <!-- 状态列 -->
        <template v-slot:status="{ row }">
          <el-switch :active-value="0" :inactive-value="1" v-model="row.status" @change="editStatus(row)" />
        </template>
        <!-- 是否推荐 -->
        <template v-slot:flag="{ row }">
          <el-tag v-if="row.flag != 0" type="success">是</el-tag>
          <el-tag v-else type="danger">否</el-tag>
        </template>

        <!-- 操作列 -->
        <template v-slot:action="{ row }">

          <!-- 如果flag = 0 推荐 否则取消 -->
          <el-button v-if="row.flag != 0"  type="text"
            @click="openRecommend(row)">推荐</el-button>
          <el-button v-else type="text" @click="openRecommend(row)">推荐</el-button>

          <!-- 如果不是禁用的 点击登录 -->
          <el-link v-if="row.status != 1" type="primary" :underline="false" @click="login(row)">
            vr制作
          </el-link>

          <el-link type="primary" :underline="false"  @click="openEdit(row)">
            修改
          </el-link>
          <el-popconfirm class="ele-action" title="确定要删除此企业吗？" @confirm="remove(row)">
            <template v-slot:reference>
              <el-link type="danger" :underline="false" >
                删除
              </el-link>
            </template>
          </el-popconfirm>
        </template>
      </ele-pro-table>
    </el-card>
  </div>
</template>

<script>
import CompanySearch from './components/company-search.vue';
import {
  pageUsers,
  removeUser,
  removeUsers,
  updateUserStatus,
  updateUserPassword,
  recommendUser
} from '@/api/system/user';

export default {
  name: 'CompanyIndex',
  components: {
    CompanySearch
  },
  data() {
    return {
      // 表格列配置  
      columns: [
        {
          columnKey: 'selection',
          type: 'selection',
          align: 'center'
        },
        {
          slot: 'avatar',
          prop: 'avatar',
          label: '企业图片',
          align: 'center',
          showOverflowTooltip: true
        },
        {
          prop: 'companyName',
          label: '企业名称',
          align: 'center',
          showOverflowTooltip: true
        },
        // 分类
        {
          prop: 'classifyName',
          label: '分类',
          align: 'center',
          showOverflowTooltip: true
        },
        {
          prop: 'username',
          label: '账号',
          align: 'center',
          showOverflowTooltip: true
        },
        {
          prop: 'passwordText',
          label: '密码',
          align: 'center',
          showOverflowTooltip: true
        },
        {
          prop: 'createTime',
          align: 'center',
          label: '创建时间',
          showOverflowTooltip: true,
        },
        {
          slot: 'status',
          prop: 'status',
          label: '状态',
          align: 'center',
          showOverflowTooltip: true
        },
        {
          slot: 'flag',
          prop: 'flag',
          label: '是否推荐',
          align: 'center',
          showOverflowTooltip: true
        },
        {
          columnKey: 'action',
          label: '操作',
          align: 'center',
          resizable: false,
          slot: 'action',
        }
      ],
      // 表格选中数据
      selection: [],
      // 当前编辑数据
      current: null,
      // 是否显示编辑弹窗
      showEdit: false,
      // 是否显示导入弹窗
      showImport: false
    };
  },
  methods: {
    /* 表格数据源 */
    datasource({ page, limit, where, order }) {
      // where 添加types=2
      where.types = 2;
      return pageUsers({ ...where, ...order, page, limit });
    },
    /* 刷新表格 */
    reload(where) {
      this.$refs.table.reload({ page: 1, where: where });
    },
    /* 打开编辑弹窗 */
    openEdit(row) {
      this.current = row;
      // 跳转到编辑页面
      this.$router.push({
        path: "./edit",
        query: { userId: row ? row.userId : '' }
      });
      // this.showEdit = true;
    },
    /* 打开导入弹窗 */
    openImport() {
      this.showImport = true;
    },
    /* 删除 */
    remove(row) {
      const loading = this.$loading({ lock: true });
      removeUser(row.userId)
        .then((msg) => {
          loading.close();
          this.$message.success(msg);
          this.reload();
        })
        .catch((e) => {
          loading.close();
          this.$message.error(e.message);
        });
    },
    // 登录 模拟提交表单
    login(row) {
      const loading = this.$loading({ lock: true });
      // a标签跳转
      console.log('https://cibvr.cinn.cn/passport/login?act=gy_login&phone='+row.username+'&password='+row.passwordText);
      window.open('https://cibvr.cinn.cn/passport/login?act=gy_login&phone='+row.username+'&password='+row.passwordText);
      // // 移除所有的form
      // const forms = document.querySelectorAll('.login-form');
      // forms.forEach((form) => {
      //   form.remove();
      // });
      // // 模拟a标签提跳转
      // const form = document.createElement('form');
      // // 添加form class
      // form.className = 'login-form';
      // form.action = 'http://gyvr.haimaitong.cn/passport/login?act=gy_login&phone='+row.username+'&password='+row.passwordText;
      // form.method = 'get';
      // // 新标签页打开
      // form.target = '_blank';
      // form.style.display = 'none';
      // document.body.appendChild(form);
      // console.log(form);
      // form.submit();
      // //2 秒后关闭loading
      setTimeout(() => {
        loading.close();
      }, 2000);
    },
    /* 批量删除 */
    removeBatch() {
      if (!this.selection.length) {
        this.$message.error('请至少选择一条数据');
        return;
      }
      this.$confirm('确定要删除选中的资讯吗?', '提示', {
        type: 'warning'
      })
        .then(() => {
          const loading = this.$loading({ lock: true });
          removeUsers(this.selection.map((d) => d.userId))
            .then((msg) => {
              loading.close();
              this.$message.success(msg);
              this.reload();
            })
            .catch((e) => {
              loading.close();
              this.$message.error(e.message);
            });
        })
        .catch(() => { });
    },
    /* 更改状态 */
    editStatus(row) {
      const loading = this.$loading({ lock: true });
      updateUserStatus(row.userId, row.status)
        .then((msg) => {
          loading.close();
          this.$message.success(msg);
        })
        .catch((e) => {
          loading.close();
          row.statusFlag = !row.statusFlag ? 1 : 0;
          this.$message.error(e.message);
        });
    },
    // 推荐
    openRecommend(row) {
      const loading = this.$loading({ lock: true });
      recommendUser({userId: row.userId})
        .then((msg) => {
          loading.close();
          this.$message.success(msg);
          this.reload();
        })
        .catch((e) => {
          loading.close();
          this.$message.error(e.message);
        });
    }
  }
};
</script>
